package com.funmi.lizip.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.funmi.lizip.common.base.R;
import com.funmi.lizip.common.base.ResultCode;
import com.funmi.lizip.common.exception.ApplicationException;
import com.funmi.lizip.entity.Task;
import com.funmi.lizip.entity.TaskItem;
import com.funmi.lizip.entity.dto.task.TaskAddRequest;
import com.funmi.lizip.entity.dto.task.TaskUpdateRequest;
import com.funmi.lizip.entity.dto.taskDelivery.TaskDeliveryQueryRequest;
import com.funmi.lizip.entity.dto.taskItem.TaskItemAddRequest;
import com.funmi.lizip.entity.dto.taskItem.TaskItemQueryRequest;
import com.funmi.lizip.entity.dto.taskItem.TaskItemUpdateRequest;
import com.funmi.lizip.entity.vo.TaskItemVo;
import com.funmi.lizip.entity.vo.TaskVo;
import com.funmi.lizip.service.TaskItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
@Api(tags = "数据采集和核查")
@RequestMapping("taskItem")
public class TaskItemController {

    @Autowired
    private TaskItemService taskItemService;

    @ApiOperation(value = "任务项的新增")
    @PostMapping("")
    public R addTaskItem(@RequestBody TaskItemAddRequest request) {
        if (request == null) {
            throw new ApplicationException(ResultCode.NOT_FOUND);
        }

        request.setCreateAt(LocalDateTime.now());
        request.setIsDelete(false);
        request.setIsRequired(true);
        TaskItem taskItem = new TaskItem();
        BeanUtils.copyProperties(request, taskItem);
        boolean save = taskItemService.save(taskItem);

        if (save) {
            return R.success().message("新增成功");
        } else {
            throw new ApplicationException("新增失败");
        }
    }


    @ApiOperation(value = "任务项的修改")
    @PutMapping("/{id}")
    public R updateTaskItem(@RequestBody(required = false) TaskItemUpdateRequest request) {
        if (request == null || request.getId() == null) {
            throw new ApplicationException(ResultCode.NOT_FOUND);
        }
        TaskItem taskItem = new TaskItem();
        BeanUtils.copyProperties(request, taskItem);

        boolean update = taskItemService.updateById(taskItem);
        if (update) {
            return R.success().message("修改成功");
        } else {
            throw new ApplicationException("修改失败");
        }
    }

    @ApiOperation(value = "任务项的删除")
    @DeleteMapping("/{id}")
    public R deleteTaskItem(@PathVariable Integer id) {
        if (id <= 0) {
            throw new ApplicationException(ResultCode.FILE_DELETE_ERROR);
        }
        boolean result = taskItemService.removeById(id);
        if (result) {
            return R.success().message("删除成功");
        } else {
            return R.fail().message("删除失败");
        }
    }

    @ApiOperation("根据任务项输入进行检索")
    @GetMapping("")
    public R listPage(TaskItemQueryRequest request) {
        long current = 1;
        long size = 5;
        TaskItem taskItemQuery = new TaskItem();
        if (request != null) {
            BeanUtils.copyProperties(request, taskItemQuery);
            current = request.getPage();
            size = request.getPageSize();
        }

        QueryWrapper<TaskItem> qw = new QueryWrapper<>(taskItemQuery);
        qw.select().orderByDesc("create_at");
        Page<TaskItem> TaskItemPage = taskItemService.page(new Page<>(current, size), qw);

        PageDTO<TaskItemVo> taskItemVoPage = new PageDTO<>(TaskItemPage.getCurrent(), TaskItemPage.getSize(), TaskItemPage.getTotal());

        List<TaskItemVo> taskItemVoList = TaskItemPage.getRecords().stream().map(taskItem -> {
            TaskItemVo taskItemVo = new TaskItemVo();
            BeanUtils.copyProperties(taskItem, taskItemVo);
            return taskItemVo;
        }).collect(Collectors.toList());
        taskItemVoPage.setRecords(taskItemVoList);
        return R.success().data(taskItemVoPage);
    }



    @ApiOperation("根据id查询任务项列表")
    @GetMapping("/{id}")
    public R listById(TaskItemQueryRequest request){
        if(request==null){
            throw new ApplicationException(ResultCode.NOT_FOUND);

        }
        TaskItem taskItem = taskItemService.listById(request);
        if(taskItem==null){

            return R.fail().message("id不存在");
        }
        return R.success().data(taskItem);
    }

}
